package cn.hill4j.tool.spring.ext.iop.dynamic;

import cn.hill4j.tool.spring.ext.iop.feign.IopFeignClientBuilder;
import cn.hill4j.tool.spring.ext.iop.feign.IopFeignTarget;
/**
  * ClassName: DymFeignIopClientFactory
  * Description: 动态feignClient bean 创建工厂
  * @param <K>  动态健值类型
  * @param <T>  接口类型
  * @author hillchen
  * Date: 2023/8/11
  * @version: 1.0
  */
public class DymFeignIopClientFactory<K,T> implements DymIopClientFactory<K,T> {
    /**
     * 接口类型
     */
    private Class<T> iopClientClass;

    /**
     * 动态健值获取器
     */
    private DymKeySelector<K, IopFeignTarget> feignTargetSelector;
    /**
     * 动态feignClient bean构造器
     */
    private IopFeignClientBuilder iopFeignClientBuilder;
    /**
     * @author hillchen
     * Description: 动态feignClient bean 创建工厂构造方法
     * Date: 2023/8/11 00:26
     * @param iopClientClass 接口类型
     * @param feignTargetSelector 动态健值获取器
     * @param iopFeignClientBuilder 动态feignClient bean构造器
     **/
    public DymFeignIopClientFactory(Class<T> iopClientClass, DymKeySelector<K, IopFeignTarget> feignTargetSelector, IopFeignClientBuilder iopFeignClientBuilder) {
        this.iopClientClass = iopClientClass;
        this.feignTargetSelector = feignTargetSelector;
        this.iopFeignClientBuilder = iopFeignClientBuilder;
    }
    /**
     * @author hillchen
     * Description: 创建动态feignClient bean
     * Date: 2023/8/11 00:28
     * @param key 动态健值
     * @return T
     **/
    public T createIopClient(K key){
        IopFeignTarget iopFeignTarget = feignTargetSelector.select(key);
        return (T)iopFeignClientBuilder.getTarget(iopFeignTarget,iopClientClass);
    }
}
